package com.crm.web;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 过滤器
 */
public class AuthorizationFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest HttpRequest = (HttpServletRequest) servletRequest;
        HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
        if (HttpRequest.getSession().getAttribute("loginUser") == null) {
            httpResponse.setContentType("text/html;charset=UTF-8");
            PrintWriter out = httpResponse.getWriter();
            out.println("<script>alert('请先进行登录，在进行后续操作！(Filter控制器)');location.href='"+HttpRequest.getContextPath()+"/login.jsp';</script>");
        } else {
            filterChain.doFilter(HttpRequest,httpResponse);
        }
    }
}
